package edu.suda.LoopCodePlus.service.impl;

import edu.suda.LoopCodePlus.entity.SubmissionRecord;
import edu.suda.LoopCodePlus.mapper.SubmissionRecordMapper;
import edu.suda.LoopCodePlus.service.SubmissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class SubmissionServiceImpl implements SubmissionService {
    @Autowired
    private SubmissionRecordMapper submissionMapper;

    //根据用户id查询其已经通过的记录
    @Override
    public Set<Integer> findAllByUserId(Integer userId,List<Integer> pid) {
        List<SubmissionRecord> submissionRecords = submissionMapper.select_SubmissionRecordByUserId(userId);
        System.out.println(submissionRecords);
        Set<Integer> ids = new HashSet<>();
        /** 遍历列表，找出其中return==“通过” */
        for (SubmissionRecord submissionRecord : submissionRecords) {
            if (submissionRecord.getPoint().equals(100) && pid.contains(submissionRecord.getProblemId())) {
                ids.add(submissionRecord.getProblemId());
            }
        }
        return ids;
    }
    @Override
    public boolean insertSubmissionRecord(SubmissionRecord submissionRecord){
        try {
            submissionMapper.insertSubmissionRecord(submissionRecord);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<SubmissionRecord> findSubmissionRecordById(Integer userId, Integer problemId){
        List<SubmissionRecord> submissionRecords=null;
        try{
            submissionRecords=submissionMapper.select_SubmissionRecordByProblemIdAndUserId(problemId, userId);
            return submissionRecords;
        }catch (Exception e){
            return submissionRecords;
        }

    }
}
